<template>
  <xtx-dialog
    :title="`${title}收货地址`"
    width="800px"
    :visible.sync="visibleDialog"
    @on-cancel="visibleDialog=false"
    @on-submit="submit"
  >
    <div class="form-item">
      <span class="label">姓名：</span>
      <input v-model="form.receiver" type="text" placeholder="收货人姓名" />
    </div>
    <div class="form-item">
      <span class="label">手机号：</span>
      <input v-model="form.contact" type="text" placeholder="输入手机号" />
    </div>
    <div class="form-item">
      <span class="label">地区：</span>
      <xtx-city
        ph="请选择所在地区"
        :level="3"
        :province-code="form.provinceCode"
        :city-code="form.cityCode"
        :area-code="form.countyCode"
        @change="changeCity"
      ></xtx-city>
    </div>
    <div class="form-item">
      <span class="label">详细地址：</span>
      <input v-model="form.address" type="text" placeholder="完善详细地址" />
    </div>
    <div class="form-item">
      <span class="label">邮政编码：</span>
      <input v-model="form.postalCode" type="text" placeholder="输入邮政编码" />
    </div>
    <div class="form-item">
      <span class="label">地址标签：</span>
      <input v-model="form.addressTags" type="text" placeholder="输入地址标签,逗号分隔" />
    </div>
  </xtx-dialog>
</template>

<script>
export default {
  name: 'address-new',
  props: {
    address: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      visibleDialog: false,
      // 表单数据
      form: {}
    }
  },
  computed: {
    title () {
      return this.form.id ? '编辑' : '新建'
    }
  },
  methods: {
    // 选择配送地区
    changeCity (e) {
      this.form.provinceCode = e.provinceCode
      this.form.cityCode = e.cityCode
      this.form.countyCode = e.areaCode
      this.form.fullLocation = e.fullLocation
    },
    open () {
      this.visibleDialog = true
      if (this.address.id) {
        this.form = { ...this.address }
      } else {
        this.form = {
          receiver: '',
          contact: '',
          provinceCode: '',
          cityCode: '',
          countyCode: '',
          address: '',
          postalCode: '',
          addressTags: '',
          fullLocation: ''
        }
      }
    },
    async submit () {
      try {
        if (this.form.id) {
          await this.$http.put(`member/address/${this.form.id}`, this.form)
        } else {
          const { data } = await this.$http.post('member/address', this.form)
          this.form.id = data.result.id
        }
        this.$message({ type: 'success', message: this.title + '地址成功' })
        this.$emit('on-complete', this.form)
        this.visibleDialog = false
      } catch (e) {
        this.$message({ type: 'error', message: this.title + '地址失败' })
      }
    }
  }
}
</script>

<style scoped lang='less'>
.form-item {
  display: flex;
  line-height: 50px;
  padding-bottom: 20px;
  .label {
    width: 150px;
    text-align: right;
    padding-right: 10px;
    color: #999;
  }
  input {
    width: 350px;
    height: 50px;
    border: 1px solid #e4e4e4;
    padding: 0 10px;
    &::placeholder {
      color: #ccc !important;
    }
  }
}
::v-deep .xtx-city {
  width: 350px;
  height: 50px;
  .curr-wrapper {
    line-height: 48px;
    padding-left: 10px;
    .selected {
      color: #333;
    }
  }
}
.xtx-dialog ::v-deep > .new-box > .body {
  overflow: visible;
}
</style>
